<?php
require_once(FRAMEWORK_DIR."/libraries/neocasta/theme.php");
class NFactory {


function &getTheme($name=''){
return NTheme::getInstance(THEME);
}

function &createDocument($config){

// 
	return NDocument::getInstance($config);


}

function &getApplication($id=null,$config=array()){
		static $instance;

		if (!is_object($instance))
		{
			//jimport( 'joomla.application.application' );
	
			if (!$id) {
				NError::raiseError(500, 'Application Instantiation Error');
			}

			$instance = NApplication::getInstance($id, $config);
		}

		return $instance;

	//return NApplication::getInstance($client,$config);
}

function &getRegistry(){
	$client='';
	return NRegistry::getInstance($client);
}




function &getTemplateEngine($type="php"){
	return NTemplateEngine::getInstance($type,array('compile_dir'=>ROOT_DIR."/templates_c/",
	'template_dir'=>ROOT_DIR."/templates/",

	'left_delimiter'=>'{{',
	'right_delimiter'=>'}}'	
	));
	
}
function &getParadigmaSmarty(){
	return NTemplateEngine::getInstance("smarty",array(
		'compile_dir'=>ROOT_DIR."/paradigma/templates_c/",
		'template_dir'=>ROOT_DIR."/paradigma/templates/",
		'plugins_dir'=>array(
			ROOT_DIR."/paradigma/smarty/libs/plugins/"
			
			)
))->engine;
	
}
function getDBS(){
	static $instance;

		if (!is_object($instance))
		{
			$instance = NFactory::_createDBSimple();
		}

		return $instance;
	}

function &_createDBSimple(){

	
		$registry=NFactory::getRegistry();
		$config=$registry->getEntry("config");

		$host 		= $config->db_host;
		$user 		= $config->db_user;
		$password 	=$config->db_password;
		$database	= $config->db_name;
		$prefix 	= $config->db_prefix;
		$driver 	= 'mysql';

		$options	= array ( 'driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix );
	
			$_dburi = "$driver://$user:$password@$host/$database?ident_prefix=catalog_";
	
		require_once FRAMEWORK_DIR."/libraries/dklab/DbSimple/Generic.php";
		
		$db =& DbSimple_Generic::connect($_dburi);

				
		return $db;
}
function &getDBO()
	{
		static $instance;

		if (!is_object($instance))
		{
			$instance = NFactory::_createDBO();
		}

		return $instance;
	}

function &_createDBO()	{
	require_once FRAMEWORK_DIR."/libraries/neocasta/database/database.php";
	require_once FRAMEWORK_DIR."/libraries/neocasta/database/table.php";
		$registry=NFactory::getRegistry();
		$config=$registry->getEntry("config");

		$host 		= $config->db_host;
		$user 		= $config->db_user;
		$password 	=$config->db_password;
		$database	= $config->db_name;
		$prefix 	= $config->db_prefix;
		$driver 	= 'mysql';

		$options	= array ( 'driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix );

		$db =& NDatabase::getInstance( $options );

		if ( NError::isError($db) ) {
			nexit('Database Error: ' . $db->toString() );
		}

		if ($db->getErrorNum() > 0) {
			NError::raiseError(500 , 'JDatabase::getInstance: Could not connect to database <br/>' . 'joomla.library:'.$db->getErrorNum().' - '.$db->getErrorMsg() );
		}

		
		return $db;
	}

	function &getConfig($file = null, $type = 'PHP')
	{
		static $instance;

		if (!is_object($instance))
		{
			if ($file === null) {
				$file = dirname(__FILE__).DS.'config.php';
			}

			$instance = NFactory::_createConfig($file, $type);
		}

		return $instance;
	}

	function &_createConfig($file, $type = 'PHP')
	{
	require_once FRAMEWORK_DIR."/libraries/neocasta/registry.php";//nimport

		require_once $file;

		
		$registry = NRegistry::getInstance();
		$registry->setEntry("config",new NConfig);

		

		return $registry;
	}
	function &getUploader($path='',$prefix="neo"){
	 require_once FRAMEWORK_DIR."/libraries/neocasta/uploader/uploader.php";
	$oUploader = NUploader::getInstance(array('path'=>ROOT_DIR."/uploads/$path",'prefix'=>$prefix));
	return $oUploader;
	
	}
	function &getMailer(){
	require_once FRAMEWORK_DIR."/libraries/phpmailer/phpmailer.php";
	return $oMailer = new PHPMailer();
	
	}
	function &getLanguage(){
	require_once FRAMEWORK_DIR."/libraries/neocasta/language/language.php";
	/* &lang=ru*/
	return NLanguage::getInstance(NRequest::getVar('lang'));
	
	}

	function &getCache(){
	static $instance_cache;
	if (!$instance_cache){
	require_once(FRAMEWORK_DIR."/libraries/cache/Lite.php");
	$options = array(
	'cacheDir' => NPATH::base()."/cache/", 
	'lifeTime' => 86400);

	$instance_cache = new Cache_Lite($options);
	}
	return $instance_cache;
	
	}
	function &getLogger_old(){
	require_once(FRAMEWORK_DIR."/libraries/neocasta/logger.php");
	return NLogger::getInstance(NPATH::base()."/logs/system.log");
	}

	function &getLogger($type = 'DEBUG'){
	//require_once('Log.php');
	if ($type=='DEBUG'){
	$log1 = &Log::singleton('firebug', '', 'PHP',
				array('buffering' => true),
				PEAR_LOG_DEBUG);
	$log2 = &Log::singleton('file', NPATH::base()."/logs/debug.log",
                           'PHP', 
                           array('mode' => 0666), PEAR_LOG_DEBUG);
 	$log = &Log::singleton('composite');
	   $log->addChild($log1);
	   $log->addChild($log2);
		return $log;
	} else {
	$log = &Log::singleton('file', NPATH::base()."/logs/system.log",
                           'PHP', 
                           array('mode' => 0666), PEAR_LOG_DEBUG);
	return $log;

	}
	
	}

	function &getRegistryFormat($format='ini'){
	
		require_once(FRAMEWORK_DIR."/libraries/neocasta/registry/format.php");
		return  NRegistryFormat::getInstance($format);
	
	}
/**
	 * Return a reference to the {@link JDate} object
	 *
	 * @access public
	 * @param mixed $time The initial time for the JDate object
	 * @param int $tzOffset The timezone offset.
	 * @return object JDate
	 * @since 1.5
	 */
	function &getDate($time = 'now', $tzOffset = 0)
	{
		require_once(FRAMEWORK_DIR."/libraries/neocasta/utilities/date.php");
		static $instances;
		static $classname;
		static $mainLocale;

		if(!isset($instances)) {
			$instances = array();
		}

	
		$classname = 'NDate';

		$key = $time . '-' . $tzOffset;

		if(!isset($instances[$classname][$key])) {
			$tmp = new $classname($time, $tzOffset);
			//We need to serialize to break the reference
			$instances[$classname][$key] = serialize($tmp);
			unset($tmp);
		}

		$date = unserialize($instances[$classname][$key]);
		return $date;
	}

	function &getUser($id = null)
	{
		require_once(FRAMEWORK_DIR."/libraries/neocasta/database/table/user.php");

		if(is_null($id))
		{
			if (!isset($_SESSION['uid'])) return NUser::getInstance();
			$instance =& NUser::getInstance($_SESSION['uid']);
			
		}
		else
		{
			$instance =& NUser::getInstance($id);
		}

		return $instance;
	}
	function &getAcl(){
		require_once(FRAMEWORK_DIR."/libraries/neocasta/base/singleton.php");
	require_once(FRAMEWORK_DIR."/libraries/tackle/tackle.php");
		return NSingleton::getInstance('tackle');
	}

}

?>